<?php

namespace App\Http\Controllers\functions;


use Illuminate\Http\Request;
use App\Models\Grantfunction;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;

class GrantfunctionController extends Controller
{
    protected $name = '权限管理';
    //查询列表字段
    protected $findlist = ['a.id', 'a.menuid', 'a.roleid', 'a.channelids', 'a.functionlist'];

    //加载当前人权限
    public function loadgrant(Request $request){
        try {
            $usr=$this->decodetoken();
            $userid=$usr->id;
            $name=$usr->name;
            $FSQL="select * from userrole
            where userid=$userid and roleid=1 and isdel=0";
            $isadmin=0;
            $temp=DB::select($FSQL);
            $grantlist=[];
            if(!empty($temp)){
                $isadmin=1;
            }
            if($name=='admin'){
                $isadmin=1;
            }
            if($isadmin==0){
                $passarr=['id','name','path','select'];
                $templist = DB::table('userrole')
                ->join('UsrGroup', 'userrole.roleid', '=', 'UsrGroup.id')
                ->join('grantfunction', 'grantfunction.roleid', '=', 'UsrGroup.UsrGroupid')
                ->select(['grantfunction.functionlist'])
                ->whereRaw(" userrole.userid=$userid and userrole.isdel=0 and UsrGroup.isdel=0")->get();
                foreach($templist as $key){
                    $data=json_decode($key->functionlist,true);
                    foreach($data as $k=>$v){
                        if(!in_array($k,$passarr)){
                            $grantlist[]=$v;     
                        }
                    }
                }
            }
            return $this->ajaxReturn(['code' => 1, 'msg' => '权限查询成功!','isadmin'=>$isadmin, 'data' =>$grantlist  ]);
        } catch (\Exception $e) {
            return $this->ajaxReturn(['code' => 0, 'msg' => $this->name . '|查询失败!','error'=>$e->getMessage()]);
        }
    }
    //查询列表
    public function findlist(Request $request)
    {
        try {
            $temp = $this->findlistdata($request);
            if(isset($temp['code'])){
                return $this->ajaxReturn($temp);
            }
            $this->log(['operator' => null, 'type' => '查看', 'billcaption' => '权限管理', 'des' => '操作人[查看]了权限管理']);
            return $this->ajaxReturn(['code' => 1, 'msg' => $this->name . '查询成功!', 'data' => $temp]);
        } catch (\Exception $e) {
            return $this->ajaxReturn(['code' => 0, 'msg' => $this->name . '查询失败：' . $e->getMessage()]);
        }
    }

    //保存
    public function save(Request $request)
    {
        try {
            $usr = $this->decodetoken();
            $param = json_decode($request->param, true);
            $id = $this->I($param['recordid'] ?? null);
            $menuid = $this->I($param['menuid'] ?? null);
            $roleid = $this->I($param['roleid'] ?? null);
            $role = $this->I($param['role'] ?? null);
            $channelids = $this->I($param['channelids'] ?? null);
            $functionlist = $this->I($param['functionlist'] ?? null);
            if (empty($roleid)) {
                return $this->ajaxReturn(['code' => 0, 'msg' => '保存失败,角色不能为空!']);
            }
            if (empty($menuid)) {
                return $this->ajaxReturn(['code' => 0, 'msg' => '保存失败,功能菜单不能为空!']);
            }
            if (empty($functionlist)) {
                return $this->ajaxReturn(['code' => 0, 'msg' => '保存失败,数据不能为空!']);
            }
            $list = [
                'roleid' => $roleid,
                'menuid' => $menuid,
                'channelids' => $channelids,
                'modifier' => $usr->name,
                'functionlist' => json_encode($functionlist)
            ];
            $type = '';
            //新增
            if (empty($id)) {
                $type = '新增';
                $list['tabnm'] = $usr->name;
                $res = Grantfunction::create($list);
                if ($res) {
                    $id = $res->id;
                } else {
                    return $this->ajaxReturn(['code' => 1, 'msg' => $this->name . '|新增失败!', 'id' => null]);
                }
            } else {//修改
                $type = '修改';
                $uptab = Grantfunction::find($id);
                $uptab->update($list);
                $res = $uptab;
            }
            //记录日志
            $this->log(['operator' => $usr->name, 'type' => $type, 'billcaption' => '权限管理', 'des' => '操作人[' . $type . ']了[' . $role . ']权限']);
            return $this->ajaxReturn(['code' => 1, 'msg' => $this->name . '保存成功!', 'id' => $id]);
        } catch (\Exception $e) {
            return $this->ajaxReturn(['code' => 0, 'msg' => $this->name . '|保存失败!','error'=>$e->getMessage()]);
        }
    }

    protected function findlistdata(Request $request)
    {
        try {
            $param = json_decode($request->param, true);
            $id = $this->I($param['id'] ?? null);
            $menuid = $this->I($param['menuid'] ?? null);
            $roleid = $this->I($param['roleid'] ?? null);
            $where = ' isdel=0';
            if(empty($menuid)){
                return ['code' => 0, 'msg' => '请选择要查询的菜单!'];   
            }
            if(empty($roleid)){
                return ['code' => 0, 'msg' => '请选择要查询的角色!'];   
            }
            $where .= " and a.menuid = '$menuid' and a.roleid = '$roleid'";
            $temp = DB::table('Grantfunction as a')
                ->select($this->findlist)
                ->whereRaw($where)
                ->get();;
            return $temp;
        } catch (\Exception $e) {
            return $this->ajaxReturn(['code' => 0, 'msg' => $this->name . '|查询失败!','error'=>$e->getMessage()]);
        }
    }
}
